package ldh.fx.adminfx.controller;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Pagination;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import ldh.fx.adminfx.controller.sub.ProductItemController;
import ldh.fx.adminfx.pojo.FxmlBean;
import ldh.fx.adminfx.pojo.Product;
import ldh.fx.adminfx.util.RegionUtil;

import java.net.URL;
import java.util.*;

public class ProductListController implements Initializable {

    @FXML private ChoiceBox<String> productOrderChoiceBox;
    @FXML private Pane productListBox;
    @FXML private Pagination productListPagination;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        productOrderChoiceBox.getItems().addAll("最佳比配", "最新宝贝", "价格");

        loadProductData();

    }

    private void loadProductData() {
        productListBox.getChildren().clear();
        productListPagination.setPageCount(10);

        List<Product> products = loadProducts(1);
        for(Product product : products) {
            FxmlBean<VBox, ProductItemController> fxmlBean = RegionUtil.loadFxmlBean("/fxml/sub/ProductItem.fxml");
            fxmlBean.getController().setProduct(product);
            productListBox.getChildren().add(fxmlBean.getNode());
        }
    }

    private List<Product> loadProducts(int page) {
        List<Product> products = new ArrayList<>();
        Random random = new Random();
        for(int i=0; i<5; i++) {
            Product p = new Product();
            p.setNew(random.nextBoolean());
            p.setName("Apple iMac Pro (27-inch with Retina 5K Display, 3.0GHz 10-core Intel Xeon W, 1TB SSD)");
            p.setImg(this.getClass().getResource("/img/product/pp.jpg").toExternalForm());
            p.setDiscountPrize(1199.5);
            p.setPrize(2238d);
            if(random.nextBoolean()) {
                p.setProgress(random.nextInt(5) + ((int)(random.nextDouble() * 10))/10.0);
            } else {
                p.setProgress(random.nextInt(5) * 1.0);
            }

            p.setEnough(true);
            p.setInfoes(Arrays.asList("16GB RAM", "1TB SSD Hard Drive", "10-core Intel Xeon", "Mac OS", "Secured"));

            products.add(p);
        }
        return products;
    }
}
